package com.zjw.zy.componse.demo.fragment

import android.util.Pair
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Surface
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.view.doOnAttach
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zjw.zy.componse.demo.base.BaseExpandRecyclerAdapter
import com.zjw.zy.componse.demo.base.BaseComposeFragment
import com.zjw.zy.componse.demo.ext.shortToast
import com.zjw.zy.componse.demo.ui.theme.MyApplicationTheme

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2023/2/28 9:08
 * @description：
 */
class ExpandListFragment : BaseComposeFragment() {

    private val rAdapter = RAdapter()

    override fun onContentView(rootView: ComposeView) {
        rootView.apply {
            doOnAttach {
                rAdapter.notifyData(
                    listOf(
                        Pair(Parent(1), listOf(Child(2, 1))),
                        Pair(
                            Parent(2), listOf(
                                Child(2, 2), Child(3, 2), Child(4, 2),
                            )
                        ),
                        Pair(
                            Parent(3), listOf(
                                Child(3, 3), Child(3, 3), Child(4, 3),
                            )
                        ),
                        Pair(
                            Parent(3), listOf(
                                Child(3, 4), Child(3, 4), Child(4, 4),
                            )
                        ),
                        Pair(
                            Parent(3), listOf(
                                Child(3, 4), Child(3, 4), Child(4, 4),
                            )
                        ),
                        Pair(
                            Parent(3), listOf(
                                Child(3, 4), Child(3, 4), Child(4, 4),
                            )
                        ),
                        Pair(
                            Parent(3), listOf(
                                Child(3, 4), Child(3, 4), Child(4, 4),
                            )
                        ),
                        Pair(
                            Parent(3), listOf(
                                Child(3, 4), Child(3, 4), Child(4, 4),
                            )
                        ),
                        Pair(
                            Parent(3), listOf(
                                Child(3, 4), Child(3, 4), Child(4, 4),
                            )
                        ),
                        Pair(
                            Parent(3), listOf(
                                Child(3, 4), Child(3, 4), Child(4, 4),
                            )
                        ),
                    )
                )

                "notify data".shortToast()
            }
        }.setContent {
            MyApplicationTheme {
                Surface(modifier = Modifier.fillMaxSize()) {
                    AndroidView(factory = {
                        RecyclerView(it).apply {
                            this.adapter = rAdapter
                            this.layoutManager = LinearLayoutManager(it)
                        }
                    }, modifier = Modifier.fillMaxSize())
                }
            }
        }
    }

    override fun onSubscribeValue() {

    }


    private class RAdapter : BaseExpandRecyclerAdapter<Parent, Child>() {

        override fun onChildClick(
            holder: RecyclerView.ViewHolder, parentPosition: Int, childPosition: Int
        ) {
            "click $parentPosition $childPosition ".shortToast()
        }

        override fun onParentClick(holder: RecyclerView.ViewHolder, parentPosition: Int) {
            "click $parentPosition ".shortToast()
        }

        override fun onBindChildViewHolder(holder: RecyclerView.ViewHolder, data: Child?) {
            data?.let {
                (holder.itemView as TextView).text = "parentId${data.parentId} selfId:${data.id} ${holder.javaClass.simpleName}"
            }
        }

        override fun onBindParentViewHolder(holder: RecyclerView.ViewHolder, data: Parent?) {
            data?.let {
                (holder.itemView as TextView).text = "selfId:${data.id} ${holder.javaClass.simpleName}"
            }
        }

        override fun onSupportParentExpand(
            parentPosition: Int, nextExpandStatus: Boolean
        ): Boolean {
            return true
        }

        override fun onParentViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
            return ParentViewHolder(TextView(parent.context))
        }

        override fun onChildViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
            return ChildViewHolder(TextView(parent.context))
        }
    }

}


data class Parent(val id: Int)
data class Child(val id: Int, val parentId: Int)

class ParentViewHolder(val view: View) : RecyclerView.ViewHolder(view)
class ChildViewHolder(val view: View) : RecyclerView.ViewHolder(view)